﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Trirand.Web.Mvc;
using OneDollarStore.Areas.Admin.Models;

namespace OneDollarStore.Areas.Admin.Controllers
{
    [Authorize(Roles = "Administrator")]
    public class LaptopController : Controller
    {
        StoreModelEntities storeDB = new StoreModelEntities();

        public ViewResult Index()
        {
            LaptopJqGridModel gridModel = new LaptopJqGridModel();
            // Set grid's parameters
            SetUpGrid(gridModel.LaptopGrid);
            return View(gridModel);
        }

        public JsonResult SearchGridDataRequested()
        {
            StoreModelEntities storeDB = new StoreModelEntities();
            LaptopJqGridModel gridModel = new LaptopJqGridModel();
            List<LaptopModel> items = (from p in storeDB.SerialProducts
                                       where p.Category.Name == "Laptop"
                                       orderby p.Name
                                       select new LaptopModel
                                       {
                                           LaptopID = p.SerialProductID,
                                           Name = p.Name,
                                           Description = p.Description,
                                           Image = p.Image,
                                           Warranty = p.Warranty,
                                           DealerPrice = p.DealerPrice,
                                           RetailPrice = p.RetailPrice,
                                           PromotionPrice = p.PromotionPrice,
                                           PromotionStartDate = p.PromotionStartDate,
                                           PromotionEndDate = p.PromotionEndDate,
                                           Quantity = p.Quantity,
                                           Highlight = p.Highlight,
                                           Category = p.Category.CategoryID,
                                           Manufacturer = p.Manufacturer.ManufacturerID,
                                           Mainboard = p.Laptop.Mainboard,
                                           CPU = p.Laptop.CPU,
                                           RAM = p.Laptop.RAM,
                                           HDD = p.Laptop.HDD,
                                           VGA = p.Laptop.VGA,
                                           CDDVD = p.Laptop.CDDVD,
                                           Screen = p.Laptop.Screen,
                                           Webcam = p.Laptop.Webcam,
                                           OperatingSystem = p.Laptop.OperatingSystem
                                       }).ToList();
            return gridModel.LaptopGrid.DataBind(items.AsQueryable());
        }

        public void EditRows()
        {
            StoreModelEntities storeDB = new StoreModelEntities();
            LaptopJqGridModel gridModel = new LaptopJqGridModel();
            var editData = gridModel.LaptopGrid.GetEditData();
            if (gridModel.LaptopGrid.AjaxCallBackMode == AjaxCallBackMode.AddRow)
            {
                int CategoryID = Convert.ToInt32(editData.RowData["Category"]);
                int ManufacturerID = Convert.ToInt32(editData.RowData["Manufacturer"]);
                // Add to SerialProduct table
                SerialProduct product = new SerialProduct();
                product.Name = editData.RowData["Name"];
                product.Description = editData.RowData["Description"];
                product.Image = editData.RowData["Image"];
                product.Warranty = Convert.ToDouble(editData.RowData["Warranty"]);
                product.DealerPrice = Convert.ToDecimal(editData.RowData["DealerPrice"]);
                product.RetailPrice = Convert.ToDecimal(editData.RowData["RetailPrice"]);
                product.PromotionPrice = Convert.ToDecimal(editData.RowData["PromotionPrice"]);
                product.PromotionStartDate = Convert.ToDateTime(editData.RowData["PromotionStartDate"]);
                product.PromotionEndDate = Convert.ToDateTime(editData.RowData["PromotionEndDate"]);
                product.Quantity = Convert.ToInt32(editData.RowData["Quantity"]);
                product.Highlight = (editData.RowData["Highlight"] == "on") ? true : false;
                product.Category = storeDB.Categories.First(d => d.CategoryID == CategoryID);
                product.Manufacturer = storeDB.Manufacturers.First(d => d.ManufacturerID == ManufacturerID);
                storeDB.AddToSerialProducts(product);
                // Add to Laptop table
                Laptop laptop = new Laptop();
                laptop.Mainboard = editData.RowData["Mainboard"];
                laptop.CPU = editData.RowData["CPU"];
                laptop.RAM = editData.RowData["RAM"];
                laptop.HDD = editData.RowData["HDD"];
                laptop.VGA = editData.RowData["VGA"];
                laptop.CDDVD = editData.RowData["CDDVD"];
                laptop.Screen = editData.RowData["Screen"];
                laptop.Webcam = editData.RowData["Webcam"];
                laptop.OperatingSystem = editData.RowData["OperatingSystem"];
                storeDB.AddToLaptops(laptop);
                storeDB.SaveChanges();
            }
            if (gridModel.LaptopGrid.AjaxCallBackMode == AjaxCallBackMode.EditRow)
            {
                int LaptopID = Convert.ToInt32(editData.RowData["LaptopID"]);
                int ManufacturerID = Convert.ToInt32(editData.RowData["Manufacturer"]);
                int CategoryID = Convert.ToInt32(editData.RowData["Category"]);
                SerialProduct product = (from p in storeDB.SerialProducts.Include("Category").Include("Manufacturer")
                                         where p.SerialProductID == LaptopID
                                         select p).First();
                product.Name = editData.RowData["Name"];
                product.Description = editData.RowData["Description"];
                product.Image = editData.RowData["Image"];
                product.Warranty = Convert.ToDouble(editData.RowData["Warranty"]);
                product.DealerPrice = Convert.ToDecimal(editData.RowData["DealerPrice"]);
                product.RetailPrice = Convert.ToDecimal(editData.RowData["RetailPrice"]);
                product.PromotionPrice = Convert.ToDecimal(editData.RowData["PromotionPrice"]);
                product.PromotionStartDate = Convert.ToDateTime(editData.RowData["PromotionStartDate"]);
                product.PromotionEndDate = Convert.ToDateTime(editData.RowData["PromotionEndDate"]);
                product.Quantity = Convert.ToInt32(editData.RowData["Quantity"]);
                product.Highlight = (editData.RowData["Highlight"] == "on") ? true : false;
                product.Category = storeDB.Categories.First(d => d.CategoryID == CategoryID);
                product.Manufacturer = storeDB.Manufacturers.First(d => d.ManufacturerID == ManufacturerID);
                Laptop laptop = (from l in storeDB.Laptops
                                 where l.LaptopID == LaptopID
                                 select l).First();
                laptop.Mainboard = editData.RowData["Mainboard"];
                laptop.CPU = editData.RowData["CPU"];
                laptop.RAM = editData.RowData["RAM"];
                laptop.HDD = editData.RowData["HDD"];
                laptop.VGA = editData.RowData["VGA"];
                laptop.CDDVD = editData.RowData["CDDVD"];
                laptop.Screen = editData.RowData["Screen"];
                laptop.Webcam = editData.RowData["Webcam"];
                laptop.OperatingSystem = editData.RowData["OperatingSystem"];
                storeDB.SaveChanges();
            }
            if (gridModel.LaptopGrid.AjaxCallBackMode == AjaxCallBackMode.DeleteRow)
            {
                int LaptopID = Convert.ToInt32(editData.RowData["LaptopID"]);
                Laptop laptop = (from l in storeDB.Laptops
                                 where l.LaptopID == LaptopID
                                 select l).First();
                storeDB.DeleteObject(laptop);
                SerialProduct product = (from p in storeDB.SerialProducts
                                         where p.SerialProductID == LaptopID
                                         select p).First();
                storeDB.DeleteObject(product);
                storeDB.SaveChanges();
            }
        }

        private void SetUpGrid(JQGrid laptopGrid)
        {
            laptopGrid.ID = "LaptopGrid";
            laptopGrid.DataUrl = Url.Action("SearchGridDataRequested");
            laptopGrid.EditUrl = Url.Action("EditRows");
            laptopGrid.ToolBarSettings.ShowAddButton = true;
            laptopGrid.ToolBarSettings.ShowEditButton = true;
            laptopGrid.ToolBarSettings.ShowDeleteButton = true;
            laptopGrid.ToolBarSettings.ShowSearchButton = true;
            laptopGrid.ToolBarSettings.ShowRefreshButton = true;
            laptopGrid.AddDialogSettings.CloseAfterAdding = true;
            laptopGrid.EditDialogSettings.CloseAfterEditing = true;
            // Setup the dropdown values for the category and manufacturer editing dropdown
            SetUpCategoryEditDropDown(laptopGrid);
            SetUpManufacturerEditDropDown(laptopGrid);
        }

        private void SetUpCategoryEditDropDown(JQGrid laptopGrid)
        {
            JQGridColumn categoryColumn = laptopGrid.Columns.Find(c => c.DataField == "Category");
            // Populate the search dropdown only on initial request, in order to optimize performance
            if (laptopGrid.AjaxCallBackMode == AjaxCallBackMode.RequestData)
            {
                StoreModelEntities storeDB = new StoreModelEntities();
                var editList = (from category in storeDB.Categories
                                select category);
                List<SelectListItem> list = new List<SelectListItem>();
                foreach (var item in editList)
                {
                    list.Add(new SelectListItem
                    {
                        Text = item.Name,
                        Value = item.CategoryID.ToString()
                    });
                }
                categoryColumn.EditList = list;
            }
        }

        private void SetUpManufacturerEditDropDown(JQGrid laptopGrid)
        {
            // Setup the grid search criteria for the columns
            JQGridColumn manufacturerColumn = laptopGrid.Columns.Find(c => c.DataField == "Manufacturer");
            // Populate the search dropdown only on initial request, in order to optimize performance
            if (laptopGrid.AjaxCallBackMode == AjaxCallBackMode.RequestData)
            {
                StoreModelEntities storeDB = new StoreModelEntities();
                var editList = (from manufacturer in storeDB.Manufacturers
                                select manufacturer);
                List<SelectListItem> list = new List<SelectListItem>();
                foreach (var item in editList)
                {
                    list.Add(new SelectListItem
                    {
                        Text = item.Name,
                        Value = item.ManufacturerID.ToString()
                    });
                }
                manufacturerColumn.EditList = list;
            }
        }
    }
}